package com.shanzhu.beadhouse.controller;

import com.shanzhu.beadhouse.common.constant.Constant;
import com.shanzhu.beadhouse.entity.base.Result;
import com.shanzhu.beadhouse.entity.query.WechatPayQuery;
import com.shanzhu.beadhouse.entity.vo.WechatPayResultVo;
import com.shanzhu.beadhouse.service.WechatPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "微信支付")
@RestController
@RequestMapping("/payment/wechat")
@PreAuthorize("@AuthorityAssert.hasAuthority('/fee/pay')")
public class WechatPayController {

    @Resource
    private WechatPayService wechatPayService;

    @PostMapping("/create")
    @ApiOperation(value = "创建微信支付订单", notes = Constant.DEVELOPER + Constant.EMPEROR_WEN)
    public Result createPayment(
            @ApiParam(value = "微信支付请求实体", required = true) @RequestBody WechatPayQuery query,
            @ApiParam(value = "接口访问请求头", required = true) @RequestHeader String token) {
        return wechatPayService.createPayment(query);
    }

    @GetMapping("/status/{orderNo}")
    @ApiOperation(value = "查询支付状态", notes = Constant.DEVELOPER + Constant.EMPEROR_WEN)
    public Result queryPaymentStatus(
            @ApiParam(value = "订单号", required = true) @PathVariable String orderNo,
            @ApiParam(value = "接口访问请求头", required = true) @RequestHeader String token) {
        return wechatPayService.queryPaymentStatus(orderNo);
    }

    @PostMapping("/notify")
    @ApiOperation(value = "微信支付回调", notes = "微信支付结果通知")
    public String handlePaymentNotify(HttpServletRequest request, HttpServletResponse response) {
        return wechatPayService.handlePaymentNotify(request, response);
    }

    @PostMapping("/close/{orderNo}")
    @ApiOperation(value = "关闭支付订单", notes = Constant.DEVELOPER + Constant.EMPEROR_WEN)
    public Result closePayment(
            @ApiParam(value = "订单号", required = true) @PathVariable String orderNo,
            @ApiParam(value = "接口访问请求头", required = true) @RequestHeader String token) {
        return wechatPayService.closePayment(orderNo);
    }

    @PostMapping("/refund/{orderNo}")
    @ApiOperation(value = "申请退款", notes = Constant.DEVELOPER + Constant.EMPEROR_WEN)
    public Result refund(
            @ApiParam(value = "订单号", required = true) @PathVariable String orderNo,
            @ApiParam(value = "退款原因") @RequestParam(required = false) String reason,
            @ApiParam(value = "接口访问请求头", required = true) @RequestHeader String token) {
        return wechatPayService.refund(orderNo, reason);
    }
} 